# 阈值
# @Author ZhangGJ
# @Date 2021/11/24 15:16

import cv2.cv2 as cv2


# 黑白渐变
def gradient():
    img = cv2.imread('../images/black.png', 0)
    t1, dst1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    t2, dst2 = cv2.threshold(img, 210, 255, cv2.THRESH_BINARY)
    cv2.imshow('img', img)
    cv2.imshow('dst1', dst1)
    cv2.imshow('dst2', dst2)
    cv2.waitKey()
    cv2.destroyAllWindows()


def gradient2():
    img = cv2.imread('../images/black.png', 0)
    t1, dst1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    t3, dst3 = cv2.threshold(img, 127, 150, cv2.THRESH_BINARY)
    cv2.imshow('dst1', dst1)
    cv2.imshow('dst3', dst3)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 反二值化处理
def gradient3():
    img = cv2.imread('../images/black.png', 0)
    t1, dst1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    t4, dst4 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
    cv2.imshow('dst1', dst1)
    cv2.imshow('dst4', dst4)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 低于阈值零处理
def gradient4():
    img = cv2.imread('../images/black.png', 0)
    t5, dst5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
    cv2.imshow('dst5', dst5)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 超出阈值零处理
def gradient5():
    img = cv2.imread('../images/black.png', 0)
    t5, dst6 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
    cv2.imshow('dst6', dst6)
    cv2.waitKey()
    cv2.destroyAllWindows()


# j截断阈值处理
def gradient6():
    img = cv2.imread('../images/black.png', 0)
    t1, dst1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    t7, dst7 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
    cv2.imshow('dst1', dst1)
    cv2.imshow('dst7', dst7)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 自适应处理
def gradient7():
    image = cv2.imread('../images/4.27.png')
    image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    t1, dst1 = cv2.threshold(image_gray, 127, 255, cv2.THRESH_BINARY)
    t2, dst2 = cv2.threshold(image_gray, 127, 255, cv2.THRESH_BINARY_INV)
    t3, dst3 = cv2.threshold(image_gray, 127, 255, cv2.THRESH_TOZERO)
    t4, dst4 = cv2.threshold(image_gray, 127, 255, cv2.THRESH_TOZERO_INV)
    t5, dst5 = cv2.threshold(image_gray, 127, 255, cv2.THRESH_TRUNC)
    cv2.imshow('BINARY', dst1)
    cv2.imshow('BINARY_INV', dst2)
    cv2.imshow('TOZERO', dst3)
    cv2.imshow('TOZERO_INV', dst4)
    cv2.imshow('TRUNC', dst5)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 自适应阈值
def gradient8():
    image = cv2.imread('../images/4.27.png')
    image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    athd_meam = cv2.adaptiveThreshold(image_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                      cv2.THRESH_BINARY, 5, 3)
    athd_gaus = cv2.adaptiveThreshold(image_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                      cv2.THRESH_BINARY, 5, 3)
    cv2.imshow('MEAN_C', athd_meam)
    cv2.imshow('GAUSSIAN_C', athd_gaus)
    cv2.waitKey()
    cv2.destroyAllWindows()


# Otsu
def gradient9():
    image = cv2.imread('../images/4.36.jpg')
    image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    t1, dst1 = cv2.threshold(image_gray, 127, 255, cv2.THRESH_BINARY)
    t2, dst2 = cv2.threshold(image_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    cv2.putText(dst2, 'best threshold: ' + str(t2), (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0),
                2)
    cv2.imshow('BINARY', dst1)
    cv2.imshow('OTSU', dst2)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 阈值处理的应用
def gradient10():
    img = cv2.imread('../images/car.jpg')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    t1, dst1 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    t2, dst2 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
    cv2.imshow('img', img)
    cv2.imshow('gray', gray)
    cv2.imshow('dst1', dst1)
    cv2.imshow('dst2', dst2)
    cv2.waitKey()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    gradient10()
